<html><head><title>/Users/Alexander/SandBox/nwscode/nwscode/misc.py</title>
        <script type="text/javascript"><!--
        
function show_line_range() {
    var href = document.location.href;
    if (href.indexOf('?') == -1) {
        return;
    }
    var qs = href.substring(href.indexOf('?')+1);
    if (qs.indexOf('#') >= 0) {
        qs = qs.substring(0, qs.indexOf('#'));
    }
    var first = qs.match(/f=(\d+)/)[1];
    var last = qs.match(/l=(\d+)/)[1];
    if (! first || ! last) {
        return;
    }
    var anchors = document.getElementsByTagName('A');
    var container = document.createElement('DIV');
    container.className = 'highlighted';
    var children = [];
    var start = null;
    var parent = null;
    var highlight = false;
    for (var i = 0; i < anchors.length; i++) {
        var el = anchors[i];
        if (el.getAttribute('name') == first) {
            start = el.previousSibling;
            parent = el.parentNode;
            highlight = true;
        }
        if (el.getAttribute('name') == last) {
            break;
        }
        if (highlight) {
            children[children.length] = el;
            el = el.nextSibling;
            while (el && el.tagName != 'A') {
                children[children.length] = el;
                el = el.nextSibling;
            }
        }
    }
    for (i=0; i<children.length; i++) {
        container.appendChild(children[i]);
    }
    if (start) {
        start.parentNode.insertBefore(container, start.nextSibling);
    } else {
        parent.insertBefore(container, parent.childNodes[0]);
    }
}

        // --></script>
        <style>
        div.python {
          color: #333
        }
        div.python a.lnum {
          color: #555;
          background-color: #eee;
          border-right: 1px solid #999;
          padding-right: 2px;
          margin-right: 4px;
        }
        div.python span.comment { color: #933 }
        div.python span.keyword { color: #a3e; font-weight: bold  }
        div.python span.op { color: #c96 }
        div.python span.string { color: #6a6 }
        div.python span.name { }
        div.python span.text { color: #333 }
        div.highlighted { background-color: #ff9; border: 1px solid #009 }
        </style></head><body onload="show_line_range()"><div class="python"><code><a class="lnum" href="#1" name="1">0001</a><span class="comment">#!/usr/bin/env&#0160;python</span><br />
<a class="lnum" href="#2" name="2">0002</a><span class="comment"></span><span class="comment">#&#0160;encoding:&#0160;utf-8</span><br />
<a class="lnum" href="#3" name="3">0003</a><span class="comment"></span><span class="string">"""</span><br />
<a class="lnum" href="#4" name="4">0004</a><span class="string">Miscellaneous&#0160;utilities.</span><br />
<a class="lnum" href="#5" name="5">0005</a><span class="string"></span><br />
<a class="lnum" href="#6" name="6">0006</a><span class="string">Created&#0160;by&#0160;Alexander&#0160;Ross&#0160;on&#0160;2006-07-20.</span><br />
<a class="lnum" href="#7" name="7">0007</a><span class="string">Copyright&#0160;(c)&#0160;2006&#0160;NOAA's&#0160;National&#0160;Weather&#0160;Service.&#0160;All&#0160;rights&#0160;reserved.</span><br />
<a class="lnum" href="#8" name="8">0008</a><span class="string">"""</span><br />
<a class="lnum" href="#9" name="9">0009</a><br />
<a class="lnum" href="#10" name="10">0010</a><span class="keyword">try</span><span class="op">:</span><br />
<a class="lnum" href="#11" name="11">0011</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">from</span>&#0160;<span class="name">datetime</span>&#0160;<span class="keyword">import</span>&#0160;<span class="name">datetime</span>&#0160;<span class="name">as</span>&#0160;<span class="name">dt</span><br />
<a class="lnum" href="#12" name="12">0012</a><span class="keyword">except</span>&#0160;<span class="name">ImportError</span><span class="op">:</span><br />
<a class="lnum" href="#13" name="13">0013</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">from</span>&#0160;<span class="name">pydatetime</span>&#0160;<span class="keyword">import</span>&#0160;<span class="name">datetime</span>&#0160;<span class="name">as</span>&#0160;<span class="name">dt</span><br />
<a class="lnum" href="#14" name="14">0014</a><br />
<a class="lnum" href="#15" name="15">0015</a><span class="keyword">from</span>&#0160;<span class="name">time</span>&#0160;<span class="keyword">import</span>&#0160;<span class="name">strptime</span><span class="op">,</span>&#0160;<span class="name">mktime</span><br />
<a class="lnum" href="#16" name="16">0016</a><br />
<a class="lnum" href="#17" name="17">0017</a><span class="keyword">class</span>&#0160;<span class="name">Bunch</span><span class="op">(</span><span class="name">dict</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#18" name="18">0018</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">def</span>&#0160;<span class="name">__init__</span><span class="op">(</span><span class="name">self</span><span class="op">,</span>&#0160;<span class="op">**</span><span class="name">kw</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#19" name="19">0019</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">dict</span><span class="op">.</span><span class="name">__init__</span><span class="op">(</span><span class="name">self</span><span class="op">,</span>&#0160;<span class="name">kw</span><span class="op">)</span><br />
<a class="lnum" href="#20" name="20">0020</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">self</span><span class="op">.</span><span class="name">__dict__</span><span class="op">.</span><span class="name">update</span><span class="op">(</span><span class="name">kw</span><span class="op">)</span><br />
<a class="lnum" href="#21" name="21">0021</a><br />
<a class="lnum" href="#22" name="22">0022</a><span class="keyword">class</span>&#0160;<span class="name">RelativeTime</span><span class="op">(</span><span class="name">object</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#23" name="23">0023</a>&#0160;&#0160;&#0160;&#0160;<span class="string">'The&#0160;time&#0160;is&#0160;relative&#0160;to&#0160;time&#0160;of&#0160;the&#0160;product&#0160;issuance.'</span><br />
<a class="lnum" href="#24" name="24">0024</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">def</span>&#0160;<span class="name">__init__</span><span class="op">(</span><span class="name">self</span><span class="op">,</span>&#0160;<span class="name">day</span><span class="op">,</span>&#0160;<span class="name">hour</span><span class="op">,</span>&#0160;<span class="name">minute</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#25" name="25">0025</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">self</span><span class="op">.</span><span class="name">day</span>&#0160;<span class="op">=</span>&#0160;<span class="name">int</span><span class="op">(</span><span class="name">day</span><span class="op">)</span><br />
<a class="lnum" href="#26" name="26">0026</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">self</span><span class="op">.</span><span class="name">hour</span>&#0160;<span class="op">=</span>&#0160;<span class="name">int</span><span class="op">(</span><span class="name">hour</span><span class="op">)</span><br />
<a class="lnum" href="#27" name="27">0027</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">self</span><span class="op">.</span><span class="name">minute</span>&#0160;<span class="op">=</span>&#0160;<span class="name">int</span><span class="op">(</span><span class="name">minute</span><span class="op">)</span><br />
<a class="lnum" href="#28" name="28">0028</a><br />
<a class="lnum" href="#29" name="29">0029</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">def</span>&#0160;<span class="name">offsetfrom</span><span class="op">(</span><span class="name">self</span><span class="op">,</span>&#0160;<span class="name">root</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#30" name="30">0030</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="string">'Returns&#0160;time&#0160;relative&#0160;to&#0160;root&#0160;where&#0160;root&#0160;is&#0160;a&#0160;datetime-like&#0160;object.'</span><br />
<a class="lnum" href="#31" name="31">0031</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">if</span>&#0160;<span class="name">self</span><span class="op">.</span><span class="name">day</span>&#0160;<span class="op">&lt;</span>&#0160;<span class="name">root</span><span class="op">.</span><span class="name">day</span><span class="op">:</span><br />
<a class="lnum" href="#32" name="32">0032</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">month</span>&#0160;<span class="op">=</span>&#0160;<span class="name">root</span><span class="op">.</span><span class="name">month</span><span class="op">%</span><span class="number">12</span>&#0160;<span class="op">+</span>&#0160;<span class="number">1</span><br />
<a class="lnum" href="#33" name="33">0033</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">if</span>&#0160;<span class="name">month</span>&#0160;<span class="op">&lt;</span>&#0160;<span class="name">root</span><span class="op">.</span><span class="name">month</span><span class="op">:</span><br />
<a class="lnum" href="#34" name="34">0034</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">year</span>&#0160;<span class="op">=</span>&#0160;<span class="name">root</span><span class="op">.</span><span class="name">year</span>&#0160;<span class="op">+</span>&#0160;<span class="number">1</span><br />
<a class="lnum" href="#35" name="35">0035</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">return</span>&#0160;<span class="name">dt</span><span class="op">(</span><span class="name">year</span><span class="op">,</span>&#0160;<span class="name">month</span><span class="op">,</span>&#0160;<span class="name">self</span><span class="op">.</span><span class="name">day</span><span class="op">,</span>&#0160;<span class="name">self</span><span class="op">.</span><span class="name">hour</span><span class="op">,</span>&#0160;<span class="name">self</span><span class="op">.</span><span class="name">minute</span><span class="op">)</span><br />
<a class="lnum" href="#36" name="36">0036</a><br />
<a class="lnum" href="#37" name="37">0037</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">def</span>&#0160;<span class="name">__cmp__</span><span class="op">(</span><span class="name">self</span><span class="op">,</span>&#0160;<span class="name">other</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#38" name="38">0038</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">raise</span>&#0160;<span class="name">ValueError</span><span class="op">(</span><span class="string">'It&#0160;makes&#0160;no&#0160;sense&#0160;to&#0160;compare&#0160;RelativeTime&#0160;objects.&#0160;'</span>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="string">'Instead,&#0160;get&#0160;a&#0160;datetime&#0160;from&#0160;the&#0160;method&#0160;'</span>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="string">'`offsetfrom`.'</span><span class="op">)</span><br />
<a class="lnum" href="#41" name="41">0041</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">def</span>&#0160;<span class="name">__repr__</span><span class="op">(</span><span class="name">self</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#42" name="42">0042</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">return</span>&#0160;<span class="name">self</span><span class="op">.</span><span class="name">__class__</span><span class="op">.</span><span class="name">__name__</span>&#0160;<span class="op">+</span>&#0160;<span class="string">'(%s,&#0160;%s,&#0160;%s)'</span>&#0160;<span class="op">%</span>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="op">(</span><span class="name">self</span><span class="op">.</span><span class="name">day</span><span class="op">,</span>&#0160;<span class="name">self</span><span class="op">.</span><span class="name">hour</span><span class="op">,</span>&#0160;<span class="name">self</span><span class="op">.</span><span class="name">minute</span><span class="op">)</span><br />
<a class="lnum" href="#44" name="44">0044</a><br />
<a class="lnum" href="#45" name="45">0045</a><span class="keyword">def</span>&#0160;<span class="name">parsevtectime</span><span class="op">(</span><span class="name">time_string</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#46" name="46">0046</a>&#0160;&#0160;&#0160;&#0160;<span class="name">time_pat</span>&#0160;<span class="op">=</span>&#0160;<span class="string">'%y%m%dT%H%MZ'</span>&#0160;<span class="comment">#&#0160;VTEC&#0160;time&#0160;code&#0160;pattern</span><br />
<a class="lnum" href="#47" name="47">0047</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">if</span>&#0160;<span class="name">time_string</span>&#0160;<span class="op">==</span>&#0160;<span class="string">'000000T0000Z'</span><span class="op">:</span><br />
<a class="lnum" href="#48" name="48">0048</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">return</span>&#0160;<span class="name">None</span><br />
<a class="lnum" href="#49" name="49">0049</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">else</span><span class="op">:</span><br />
<a class="lnum" href="#50" name="50">0050</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">return</span>&#0160;<span class="name">dt</span><span class="op">.</span><span class="name">utcfromtimestamp</span><span class="op">(</span><span class="name">mktime</span><span class="op">(</span><span class="name">strptime</span><span class="op">(</span><span class="name">time_string</span><span class="op">,</span>&#0160;<span class="name">time_pat</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></code></div></body></html>